﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;

public partial class Combat_NewExam : System.Web.UI.Page
{
    private Guid taskID;
    private string departmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                btnOK.Text = "保存修改";
                btnOK.CommandArgument = Request["ID"];
            }
            Bind();
            
        }
    }

  
    protected void btnOK_Click(object sender, EventArgs e)
    {
        departmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;       
        try
        {
            TaskBaseInfo taskInfo = new TaskBaseInfo();
            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                taskInfo = TaskBase.GetTaskBase(new Guid(btnOK.CommandArgument));
                taskID = taskInfo.TaskID;
                taskInfo.TaskTitle = txtTaskTitle.Text;
                taskInfo.StartDate = DateTime.Parse(txtStartDate.Text.Trim());
		DateTime dt=DateTime.Parse(txtEndDate.Text.Trim());
                taskInfo.EndDate = new DateTime(dt.Year,dt.Month,dt.Day,17,30,0);

                List<TaskBrigadeInfo> brigaList = TaskBrigade.GetList(" TaskID='" + taskID + "'");

                foreach (ListItem item in chkList.Items)
                {
                    TaskBrigadeInfo temp = brigaList.Where(o => o.DepartmentID == item.Value).FirstOrDefault();
                    if (temp != null && item.Selected == false)
                    {
                        TaskBrigade.Delete(temp.ID);
                    }

                    if (item.Selected && temp == null)
                    {
                        TaskBrigadeInfo brigade = new TaskBrigadeInfo();
                        brigade.ID = Guid.NewGuid();
                        brigade.IsAccept = false;
                        brigade.AcceptUser = "";
                        brigade.IsOn = false;
                        brigade.SetOnDate = DateTime.MaxValue;
                        brigade.SetOnUser = User.Identity.Name;
                        brigade.TaskID = taskID;
                        brigade.AcceptDate = DateTime.MaxValue;
                        brigade.DepartmentID = item.Value;
                        TaskBrigade.Insert(brigade);
                    }
                }
                TaskBase.Update(taskInfo);
                Response.Write("<script>alert('保存成功')</script>");
                //btnOK.CommandArgument = "";
                //btnOK.Text = "继续并添加训练项";
                //lblPrompt.Text = "修改成功";
                return;
            }
            else
            {
                taskID = Guid.NewGuid();
                taskInfo.TaskID = taskID;
                taskInfo.TaskTitle = txtTaskTitle.Text;
                taskInfo.StartDate = DateTime.Parse(DateTime.Parse(txtStartDate.Text.Trim()).ToString("yyyy-MM-dd 23:59:59"));
                taskInfo.EndDate = DateTime.Parse(DateTime.Parse(txtEndDate.Text.Trim()).ToString("yyyy-MM-dd 00:00:00"));
                taskInfo.IsOn = false;
                taskInfo.SetOnDate = DateTime.MaxValue;
                taskInfo.CreateUser = User.Identity.Name;
                taskInfo.CreateDepartmentID = departmentID;
                taskInfo.CreateDate = DateTime.Now;
                TaskBase.Insert(taskInfo);
                foreach (ListItem lst in chkList.Items)
                {
                    if (lst.Selected)
                    {
                        TaskBrigadeInfo brigade = new TaskBrigadeInfo();
                        brigade.ID = Guid.NewGuid();
                        brigade.IsAccept = false;
                        brigade.AcceptUser = "";
                        brigade.IsOn = false;
                        brigade.SetOnDate = DateTime.MaxValue;
                        brigade.SetOnUser = User.Identity.Name;
                        brigade.TaskID = taskID;
                        brigade.AcceptDate = DateTime.MaxValue;
                        brigade.DepartmentID = lst.Value;
                        TaskBrigade.Insert(brigade);
                    }
                }
               
            }
        }
        catch (Exception ex)
        {
            
        }
        //ListPageContral1.SetFirstPage();
        Response.Redirect("NewExam2.aspx?ID=" + taskID);
        //BindTask(0);
    }
    private void Bind()
    {
        chkList.DataSource = Yipusoft.Account.BLL.Department.GetList(" Right(DepartmentID,3)='000'");
        chkList.DataTextField = "FullName";
        chkList.DataValueField = "DepartmentID";
        chkList.DataBind();
        
        if (string.IsNullOrEmpty(Request["ID"]))
        {
            txtTaskTitle.Text = DateTime.Now.ToString("yyyy年MM月") + "训练任务";
            txtStartDate.Text = DateTime.Now.ToString("yyyy-MM-5");
            txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-27");
            foreach (ListItem chk in chkList.Items)
            {
                chk.Selected = true;
            }
        }
        else
        {
            TaskBaseInfo info = TaskBase.GetTaskBase(new Guid(Request["ID"]));
            if (info != null)
            {
                txtTaskTitle.Text = info.TaskTitle;
                txtStartDate.Text = info.StartDate.ToString("yyyy-MM-dd");
                txtEndDate.Text = info.EndDate.ToString("yyyy-MM-dd");

                List<TaskBrigadeInfo> brigade = TaskBrigade.GetList(" TaskID='"+Request["ID"]+"'");
                if (brigade == null)
                    return;
                foreach (ListItem item in chkList.Items)
                { 
                    item.Selected=brigade.Where(o=>o.DepartmentID==item.Value).Count()>0;
                }
            }
        }
       
    }

    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("TaskList.aspx");
    }
}
